/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.util.translator.lom2owl.owl;

import org.ieee.ltsc.lom.LOM;
import org.ieee.ltsc.lom.LOMUtil;

import java.io.*;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.RDFNode;

/**
 * 
 * Translates a LOM technical requirement item to its representation in OWL. 
 */
public class TR2TRTranslator extends ElementTranslatorImpl {

    private String category =
            new String();
    private String attributeName =
            new String();
    private String element =
            new String();

    /** Creates a new instance of TR2TRTranslator */
    public TR2TRTranslator(String elementCategory, String elementElement,
            String elementAttribute) {
        this.category =
                elementCategory;
        this.attributeName =
                elementAttribute;
        this.element =
                elementElement;
    }

    /**
     * This method translates a LOM technical requirement item to its representation 
     * in OWL. 
     * @param lom The metadata record
     * @param ont The ontology
     * @param inst The instance of learning Object
     * @param nsName The namespace
     * @param errorWriter The error file
     */
    public void apply(LOM lom, OntModel ont, Individual inst, String nsName,
            PrintWriter errorWriter) {

        // 1) Get the information from lom xml file

        //4.4 Requirement

        String minVersionLOM =
                "";
        String maxVersionLOM =
                "";
        String sourceNameLOM =
                "";
        String valueNameLOM =
                "";
        String sourceTypeLOM =
                "";
        String valueTypeLOM =
                "";
        String typeLOM =
                "";

        int i =
                0;
        OntClass technicalClass =
                ont.getOntClass(nsName +
                "#lomTechnicalRequirement");
        OntClass singleTRClass =
                ont.getOntClass(nsName +
                "#singleTechnicalRequirement");
        DatatypeProperty minVersionProperty =
                ont.getDatatypeProperty(nsName +
                "#minVersion");
        DatatypeProperty maxVersionProperty =
                ont.getDatatypeProperty(nsName +
                "#maxVersion");
        DatatypeProperty requirementNameProperty =
                ont.getDatatypeProperty(nsName + "#requirementName");
        DatatypeProperty typeProperty =
                ont.getDatatypeProperty(nsName +
                "#hasType");
        ObjectProperty compositeProperty =
                ont.getObjectProperty(nsName +
                "#isCompositeOf");
        ObjectProperty technicalRequirementProperty =
                ont.getObjectProperty(nsName + "#hasTechnicalRequirement");

        String instance =
                null;
        String instance2 =
                null;

        while (LOMUtil.getTechnicalRequirement(lom, i) != null) {

            instance =
                    "#TR" +
                    inst.getLocalName() + "-" + String.valueOf(i);

            Individual technicalInstance =
                    ont.createIndividual(nsName +
                    instance, technicalClass);
            int j =
                    0;
            while (LOMUtil.getTechnicalRequirementOrComposite(lom, i, j) != null) {
                //singleTechnicalRequirement
                instance2 =
                        "#TRoC" +
                        inst.getLocalName() + "-" + String.valueOf(j);
                Individual singleTRInstance =
                        ont.createIndividual(nsName +
                        instance2, singleTRClass);
                //min
                minVersionLOM =
                        LOMUtil.getString(LOMUtil.getTechnicalRequirementOrCompositeMinimumVersion(lom,
                        i, j));
                if (minVersionLOM != null) {
                    RDFNode minVersionValue =
                            ont.createTypedLiteral(minVersionLOM);
                    singleTRInstance.setPropertyValue(minVersionProperty,
                            minVersionValue);
                }
                //max
                maxVersionLOM =
                        LOMUtil.getString(LOMUtil.getTechnicalRequirementOrCompositeMaximumVersion(lom,
                        i, j));
                if (maxVersionLOM != null) {
                    RDFNode maxVersionValue =
                            ont.createTypedLiteral(maxVersionLOM);
                    singleTRInstance.setPropertyValue(maxVersionProperty,
                            maxVersionValue);
                }
                //name                
                sourceNameLOM =
                        String.valueOf(LOMUtil.getSource(LOMUtil.getTechnicalRequirementOrCompositeName(lom,
                        i, j)));
                valueNameLOM =
                        String.valueOf(LOMUtil.getValue(LOMUtil.getTechnicalRequirementOrCompositeName(lom,
                        i, j)));
                if (valueNameLOM != null) {
                    RDFNode technicalRequirementValue =
                            ont.createTypedLiteral(valueNameLOM);
                    singleTRInstance.setPropertyValue(requirementNameProperty,
                            technicalRequirementValue);
                } else {
                    String log =
                            "The Name field of the Technical category must contain a value";
                    errorWriter.println(log);
                }
                //type
                sourceTypeLOM =
                        String.valueOf(LOMUtil.getSource(LOMUtil.getTechnicalRequirementOrCompositeType(lom,
                        i, j)));
                valueTypeLOM =
                        String.valueOf(LOMUtil.getValue(LOMUtil.getTechnicalRequirementOrCompositeType(lom,
                        i, j)));
                if (valueTypeLOM != null) {
                    RDFNode typeValue =
                            ont.createTypedLiteral(valueTypeLOM);
                    singleTRInstance.setPropertyValue(typeProperty, typeValue);
                } else {
                    String log =
                            "The Type field of the Technical category must contain a value";
                    errorWriter.println(log);
                }
                technicalInstance.addProperty(compositeProperty,
                        singleTRInstance);
                j++;
            }
            inst.addProperty(technicalRequirementProperty, technicalInstance);

            i++;
        }

    }
}








